package com.first.nell.components.bill

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.base.BaseFormAction
import com.first.nell.databinding.ViewFirstNellBillTableGridBinding
import com.first.nell.extend.addFirstNellBoldFirstNellStyle
import com.first.nell.extend.addFirstNellNormalFirstNellStyle
import com.first.nell.extend.addFirstNellTipsFirstNellStyle
import com.first.nell.utils.TranslateUtil

/**
<declare-styleable name="FirstNellBillTable">
<attr name="cwbtgTitle" format="string" />
<attr name="cwbtgDetail" format="string" />
<attr name="cwbtgTitleColor" format="color" />
<attr name="cwbtgDetailColor" format="color" />
<attr name="cwbtgTitleSize" format="dimension" />
<attr name="cwbtgDetailSize" format="dimension" />
<attr name="cwbtgBottomLineHidden" format="boolean" />
<attr name="cwbtgIsDetailBold" format="boolean" />
<attr name="cwbtgIsTitleBold" format="boolean" />
</declare-styleable>
 * */
class GridBill : RelativeLayout, BaseFormAction {

    lateinit var mFirstNellViewFirstNellBinding: ViewFirstNellBillTableGridBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var cwbtgTitleColor: Int = 0
    private var cwbtgDetailColor: Int = 0
    private var cwbtgTitle: String? = null
    private var cwbtgDetail: String? = null
    private var cwbtgTitleSize: Float = 0F
    private var cwbtgDetailSize: Float = 0F
    private var cwbtgBottomLineHidden: Boolean = true
    private var cwbtgRightLineHidden: Boolean = true
    private var cwbtgIsDetailBold: Boolean = true
    private var cwbtgIsTitleBold: Boolean = true

    private var mFirstNellSupportFirstNellFragmentFirstNellManager: FragmentManager? = null

    private var mFirstNellTempFirstNellInputFirstNellValue: String = ""
    private var mFirstNellOnFirstNellActionFirstNellClickFirstNellListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.GridBill)
                cwbtgTitleColor = array.getColor(
                    R.styleable.GridBill_cwbtgTitleColor,
                    ContextCompat.getColor(cxt, R.color.black_6001)
                )
                cwbtgTitle =
                    array.getString(R.styleable.GridBill_cwbtgTitle)

                cwbtgDetailColor = array.getColor(
                    R.styleable.GridBill_cwbtgDetailColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwbtgDetail =
                    array.getString(R.styleable.GridBill_cwbtgDetail)

                cwbtgTitleSize = array.getDimension(
                    R.styleable.GridBill_cwbtgTitleSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_12
                    ).toFloat()
                )

                cwbtgDetailSize = array.getDimension(
                    R.styleable.GridBill_cwbtgDetailSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwbtgBottomLineHidden =
                    array.getBoolean(R.styleable.GridBill_cwbtgBottomLineHidden, true)

                cwbtgRightLineHidden =
                    array.getBoolean(R.styleable.GridBill_cwbtgRightLineHidden, true)

                cwbtgIsDetailBold =
                    array.getBoolean(R.styleable.GridBill_cwbtgIsDetailBold, true)

                cwbtgIsTitleBold =
                    array.getBoolean(R.styleable.GridBill_cwbtgIsTitleBold, true)

                array.recycle()
            }
        }
        context?.let {
            vFirstNellRootFirstNellView =
                View.inflate(it, R.layout.view_first_nell_bill_table_grid, this)
            mFirstNellViewFirstNellBinding =
                ViewFirstNellBillTableGridBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.textBillTableTitle.text = (cwbtgTitle)
            mFirstNellViewFirstNellBinding.textBillTableTitle.setTextColor(cwbtgTitleColor)
            mFirstNellViewFirstNellBinding.textBillTableTitle.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtgTitleSize
            )

            mFirstNellViewFirstNellBinding.textBillTableDetail.text = (cwbtgDetail)
            mFirstNellViewFirstNellBinding.textBillTableDetail.setTextColor(cwbtgDetailColor)
            mFirstNellViewFirstNellBinding.textBillTableDetail.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtgDetailSize
            )

            if (cwbtgBottomLineHidden) {
                mFirstNellViewFirstNellBinding.vBillTableBottomLine.visibility = GONE
            } else {
                mFirstNellViewFirstNellBinding.vBillTableBottomLine.visibility = VISIBLE
            }

            if (cwbtgRightLineHidden) {
                mFirstNellViewFirstNellBinding.vBillTableRightLine.visibility = GONE
            } else {
                mFirstNellViewFirstNellBinding.vBillTableRightLine.visibility = VISIBLE
            }

            if (cwbtgIsTitleBold) {
                mFirstNellViewFirstNellBinding.textBillTableTitle.addFirstNellBoldFirstNellStyle()
            } else {
                mFirstNellViewFirstNellBinding.textBillTableTitle.addFirstNellNormalFirstNellStyle()
            }

            if (cwbtgIsDetailBold) {
                mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellBoldFirstNellStyle()
            } else {
                mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellNormalFirstNellStyle()
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setFirstNellContent(content: String) {
        cwbtgDetail = content
        mFirstNellViewFirstNellBinding.textBillTableDetail.text = (cwbtgDetail)
    }

    override fun setFirstNellHint(hint: String) {

    }

    override fun setFirstNellTitle(title: String) {
        cwbtgTitle = title
        mFirstNellViewFirstNellBinding.textBillTableTitle.text = (cwbtgTitle)
    }

    override fun setFirstNellDetail(detail: String) {
        cwbtgDetail = detail
        mFirstNellViewFirstNellBinding.textBillTableDetail.text = (cwbtgDetail)

        //对货币、天数做一个灰色处理
        val currency = TranslateUtil.getFirstNellCountryFirstNellCurrency()
        mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellTipsFirstNellStyle(
            currency,
            0.8F,
            ContextCompat.getColor(context, R.color.black_text_detail)
        )

        val perDay = TranslateUtil.getGarlicPerFirstNellDay()
        mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellTipsFirstNellStyle(
            perDay,
            0.8F,
            ContextCompat.getColor(context, R.color.black_text_detail)
        )
    }

    override fun setFirstNellDetailFirstNellColor(detailColor: Int) {
        cwbtgDetailColor = detailColor
        mFirstNellViewFirstNellBinding.textBillTableDetail.setTextColor(cwbtgDetailColor)
    }

    override fun setFirstNellTitleFirstNellColor(titleColor: Int) {
        cwbtgTitleColor = titleColor
        mFirstNellViewFirstNellBinding.textBillTableTitle.setTextColor(cwbtgTitleColor)
    }

    override fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {
        mFirstNellSupportFirstNellFragmentFirstNellManager = manager
    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {
        mFirstNellOnFirstNellActionFirstNellClickFirstNellListener = listener
    }
}